#include "TutorApp.h"
#include <QString>
#include <QVector4D>

#ifdef WIN32
#ifdef Tutorial1_EXPORTS
#define TUTORAPP_API __declspec(dllexport)
#else
#define TUTORAPP_API __declspec(dllimport)
#endif
#else
#define TUTORAPP_API
#endif

extern "C" TUTORAPP_API Blobz::BaseApplication* GetInstance()
{
	Tutorial::TutorApp* pTutorial = new Tutorial::TutorApp();
	return (Blobz::BaseApplication*)pTutorial;
}

extern "C" TUTORAPP_API unsigned int GetMajorReleaseVersion()
{
	return NUI_RELEASE_MAJOR_VER;
}

extern "C" TUTORAPP_API unsigned int GetMinorReleaseVersion()
{
	return NUI_RELEASE_MINOR_VER;
}

extern "C" TUTORAPP_API const char* GetAppName()
{
	return "Tutorial";
}

namespace Tutorial
{
	/**
	* Constructor
	*/
	TutorApp::TutorApp():
			mNumberOfFingers(0),
			mHelloMessage("Touch the screen and watch the counter increase.") 
	{
	}

	/**
	* Destructor
	*/
	TutorApp::~TutorApp()
	{
	}

	/**
	* Load the objects
	*/
	bool TutorApp::onInitialize()
	{
		// Tell the application to use a fixed aspect ratio so that our view content is not stretched
		getWindow().setUseFixedRatio(true);
		
		// Load the background image
		// The bool in the end of the load function states if the texture should be loaded synchronously (true) or asynchronously (false)
		// getApplicationDirectory returns the path to the apps data directory.
		mBackgroundTexture.load(getApplicationDirectory()+"bg.png", true);

		// Load the default system font (specified in the configuration)
		mExampleFont.loadDefaultSystemFont();

		// Set the font render scale
		mExampleFont.setRenderScale(0.03f*getWindow().getWindowAspect());

		return true;
	}

	/**
	* onReload Handler
	*/
	void TutorApp::onReload()
	{
	}

	/**
	* Unload all objects
	*/
	void TutorApp::onDeinitialize()
	{
		mBackgroundTexture.unload();
		mExampleFont.unload();
	}

	/**
	* Triggered on every frame update
	* \param inFrameTime Time since last update ( in seconds )
	*/
	void TutorApp::onUpdate(float inFrameTime)
	{
	}


	/**
	* Render the graphics and other opengl job here
	*/
	void TutorApp::onDraw()
	{
		// Tell Snowflake that we want to start rendering
		getWindow().enableRendering();

		// Screen setup, part of the Snowflake framework
		getWindow().defaultRenderSetup();

		/*
			You can also send in a Blobz::Vector4 to defaultRenderSetup to change the glClearColor
			mWindow.defaultRenderSetup(Blobz::Vector4(1,0,1,1); as an example
		*/

		// Render the background texture
		const QVector2D windowSize(getWindow().getWindowAspect(), 1.0f);
		const QVector2D mPosition(0.0f, 0.0f);

		// This segment contains a mixed part of OpenGL and basic vector math
		// Enables alpha blending
		glEnable(GL_BLEND);
		glDisable(GL_DEPTH_TEST);
		glEnable(GL_TEXTURE_2D);

		// Binds the texture ID to OpenGL so it can be used
		glBindTexture(GL_TEXTURE_2D, mBackgroundTexture.getID());
		// Begin drawing GLQuads
		glBegin(GL_QUADS);
		glColor4f(1, 1, 1, 1);
			glTexCoord2f(0,1);
			glVertex2f(mPosition.x(), mPosition.y());
			glTexCoord2f(1,1);
			glVertex2f(mPosition.x()+windowSize.x(), mPosition.y());
			glTexCoord2f(1,0);
			glVertex2f(mPosition.x()+windowSize.x(), mPosition.y()+windowSize.y());
			glTexCoord2f(0,0);
			glVertex2f(mPosition.x(), mPosition.y()+windowSize.y());
		glEnd();
		glDisable(GL_TEXTURE_2D);

		// Render the title text
		// The text is displayed with the color of the last call of glColor, change color to black(0,0,0,1)
		glColor4f(0, 0, 0, 1);
		mExampleFont.print(mHelloMessage, windowSize.x()*0.5f, 0.7f);

		// Render the counter
		QString counterText = "Detected fingers: " + QString::number(mNumberOfFingers);
		mExampleFont.print(counterText, windowSize.x()*0.5f, 0.5f);

		glEnable(GL_DEPTH_TEST);
		glDisable(GL_BLEND);

		// Tell Snowflake that we're done rendering
		getWindow().disableRendering();
	}

	/**
	* Triggered when there is a finger down event on the screen
	* \param inData holds the touch information
	*/
	void TutorApp::onFingerDown(const TouchListener::TouchData& inData)
	{
		++mNumberOfFingers;
	}

	/**
	* Triggered when there is a finger up event on the screen
	* \param inData holds the touch information
	*/
	void TutorApp::onFingerUp(const TouchListener::TouchData& inData)
	{
		if(mNumberOfFingers > 0)
			--mNumberOfFingers;
	}

	/**
	* Triggered when there is a finger update event on the screen
	* \param inData holds the touch information
	*/
	void TutorApp::onFingerUpdate(const TouchListener::TouchData& inData)
	{
	}

	/**
	* Triggered when there is a finger moves out of the screen or application window
	* \param inData holds the touch information
	*/
	void TutorApp::onFingerExit(const TouchListener::TouchData& inData)
	{
		onFingerUp(inData);
	}
}
